package org.boulderfoodrescue.webapp.client.options;

import org.boulderfoodrescue.webapp.client.resources.Resources;
import org.boulderfoodrescue.webapp.shared.UserAccountDetails.AccessLevel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Widget;

public class OptionsSidebar extends Composite{

  private static ProfileSidebarUiBinder uiBinder = GWT
      .create(ProfileSidebarUiBinder.class);

  interface ProfileSidebarUiBinder extends UiBinder<Widget, OptionsSidebar> {
  }

  @UiField
  Hyperlink myinfo;
  @UiField
  Hyperlink volunteerinfo;
  @UiField
  Hyperlink manageUsers;
  Hyperlink last;
  public OptionsSidebar(AccessLevel accessLevel) {
    initWidget(uiBinder.createAndBindUi(this));
    if(accessLevel.compareTo(AccessLevel.Admin) >= 0)
    	manageUsers.setVisible(true);
    else
    	manageUsers.setVisible(false);
    if(accessLevel.compareTo(AccessLevel.Volunteer) >= 0)
    	volunteerinfo.setVisible(true);
    else
    	volunteerinfo.setVisible(false);
    
    myinfo.addStyleName(Resources.INSTANCE.profileSidebar().selected());
    last = myinfo;
    History.addValueChangeHandler(new ValueChangeHandler<String>(){
      @Override
      public void onValueChange(ValueChangeEvent<String> event) {
        onSectionChange(event.getValue());
      }
    });
    onSectionChange(History.getToken());
  }
  public void onSectionChange(String section)
  {
    last.removeStyleName(Resources.INSTANCE.profileSidebar().selected());
    if(section.equals(myinfo.getTargetHistoryToken())||section.equals("profile"))
      last = myinfo;
    else if(section.equals(volunteerinfo.getTargetHistoryToken()))
      last = volunteerinfo;
    else if(section.equals(manageUsers.getTargetHistoryToken()))
      last = manageUsers;
    last.addStyleName(Resources.INSTANCE.profileSidebar().selected());
  }
}
